<template>
	<view class="content">
		<scroll-view scroll-y class="left-aside">
			<view v-for="item in compList" :key="item.attr.title[1]" class="f-item b-b"
				:class="{active: item.attr.title[1] === currentId}" @click="tabtap(item)">
				<view class="title">{{item.attr.title[0]}}</view>
				<view class="sub-title">{{item.attr.title[1]}}</view>
			</view>
		</scroll-view>
		<scroll-view scroll-with-animation scroll-y class="right-aside" :scroll-into-view="scrollIntoView">
			<view v-for="item in compList" :key="item.attr.compId" class="s-list" :id="'main-'+item.attr.title[1]">
				<text class="s-item">{{item.attr.title[0]}} {{item.attr.title[1]}}</text>
				<Draggable class="t-list" v-model="item.child" item-key="attr.compId"
					:group="{ name: 'component', pull: 'clone', put: false }" :sort="false">
					<template v-for="(sv,sk) in item.child">
						<component class="t-item"  :is="sv.tag" v-bind="sv.attr" v-on="sv.event">
							<block v-if="typeof sv.child !='undefined'">
								{{sv.child[0].text}}
							</block>
							<component v-if="sv.child" v-for="(tv,tk) in sv.child" :key="tk" :is="tv.tag" v-bind="tv.attr"
								v-on="tv.event">
								<block v-if="typeof tv.child !='undefined'">
									{{tv.child[0].text}}
								</block>
							</component>
						</component>
					</template>
				</Draggable>
			</view>
		</scroll-view>
		<u-button v-show="false" />
		<u-image v-show="false" />
		<u-text v-show="false" />
	</view>
</template>

<script>
	import Vue from "vue"
	import compiler from 'vue-template-compiler'
	import uLayout from './components/uLayout'
	import uButton from './components/uButton'
	import uImage from './components/uImage'
	import uText from './components/uText'
	import Draggable from "vuedraggable"
	export default {
		components: {
			Draggable,
		},
		data() {
			return {
				scrollIntoView: '',
				currentId: '',
				compList: [
					{
						categoryTitle:"基础组件",
						compList:[
							{
							   compTitle:"按钮",
							   icon:""	   
							}
						]
					}
				],
				src:"https://cdn.uviewui.com/uview/album/1.jpg",
			}
		},
		created() {
			this.init()
		},
		methods: {
			init() { 
				let compList = [
					...uButton.getJson(),
					...uText.getJson(),
					...uImage.getJson(),
				]
				compList.map(fv=>{
					fv.child.map(sv=>{
						console.log(sv.tag)
						Vue.component(sv.tag)
					})
				})
				if(compList.length){
					this.currentId = compList[0].attr.title[1]
					this.compList = compList
				}
			},
			//一级分类点击
			tabtap(item) {
				this.currentId = item.attr.title[1];
				this.scrollIntoView = 'main-' + item.attr.title[1];
			},
		}
	}
</script>

<style lang='scss'>
	page,
	.content {
		height: 85vh;
		display: flex;
	}

	.left-aside {
		flex-shrink: 0;
		width: 80px;
		height: 100%;
		position: relative;

		&:before {
			content: '';
			position: absolute;
			right: 0;
			top: 0;
			transform: translateY(-50%);
			height: 100vw;
			width: 1px;
			background-color: #ccc;
			border-radius: 0 4px 4px 0;
			opacity: .8;
		}

		.title {
			letter-spacing: 2px;
			font-size: 16px;
		}

		.sub-title {
			font-size: 12px;
		}
	}

	.f-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		width: 100%;
		min-height: 55px;
		font-size: 28px;
		color: #333;
		position: relative;
		box-shadow: 0 1px 4px #ccc;

		&.active {
			color: $uni-theme-color;

			&:before {
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				height: 36px;
				width: 4px;
				background-color: $uni-theme-color;
				border-radius: 0 2px 2px 0;
				opacity: .8;
			}
		}
	}

	.right-aside {
		flex: 1;
		overflow: hidden;
	}

	.s-item {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 35px;
		padding-top: 8px;
		font-size: 15px;
		color: #555;
	}

	.t-list {
		width: 100%;
		background: #ececec;
        padding:15px 10px;
		&:after {
			content: '';
			flex: 99;
			height: 0;
		}
	}

	.t-item {
		margin-bottom: 20px;
	}
</style>
